﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Manager.App_Code;
using System.Linq;
using Manager.Common;
using System.Data.Linq;

namespace Manager.FormAdd_Edit
{
    public partial class frm_ChangePassword : DevExpress.XtraEditors.XtraForm
    {
        NewPearl_HotelDataContext db = new NewPearl_HotelDataContext();
        NguoiDung user;

        public frm_ChangePassword()
        {
            InitializeComponent();
        }

        public frm_ChangePassword(NguoiDung _user)
        {
            InitializeComponent();
            user = _user;
        }

        private void btnThayDoi_Click(object sender, EventArgs e)
        {
            
            if (txtUsername.Text == "")
            {
                errorProvider1.SetError(txtUsername, "Bắt buộc nhập!");
            }
            else
            {
                errorProvider1.SetError(txtUsername, "");
            }


            if(txtPass.Text =="")
            {
                errorProvider1.SetError(txtPass, "Bắt buộc nhập!");
            }
            else
            {
                errorProvider1.SetError(txtPass, "");
            }

            if (txtpassnew.Text == "")
            {
                errorProvider1.SetError(txtpassnew, "Bắt buộc nhập!");
            }
            else
            {
                errorProvider1.SetError(txtpassnew, "");
            }

            if (txtrepassnew.Text == "")
            {
                errorProvider1.SetError(txtrepassnew, "Bắt buộc nhập!");
            }
            else
            {
                errorProvider1.SetError(txtrepassnew, "");
            }
            if (txtUsername.Text != "" && txtpassnew.Text != "" && txtrepassnew.Text != "" && txtPass.Text != "" && txtpassnew.Text == txtrepassnew.Text)
            {
                var user = db.NguoiDungs.Single(i => i.Username == txtUsername.Text);
                if (user != null)
                {
                    if (user.Password == BitConverter.ToString(CommonFunction.encryptDataMD5(txtPass.Text)))
                    {
                        user.Password = BitConverter.ToString(CommonFunction.encryptDataMD5(txtpassnew.Text));
                        db.SubmitChanges();
                        db.Refresh(RefreshMode.OverwriteCurrentValues, db.NguoiDungs);
                        MessageBox.Show("Thay đổi thành công!");
                    }
                    else
                    {
                        MessageBox.Show("Sai mật khẩu!");
                        txtPass.Focus();
                    }
                }
                else
                {
                    MessageBox.Show("Tên đăng nhập ko đúng!");
                    txtUsername.Focus();
                }
            }
            else
            {
                errorProvider1.SetError(txtrepassnew, "Mật khẩu nhập lại phải giống mật khẩu nhập mới!");
                txtrepassnew.Focus();
            }
        }

        private void txtPass_TextChanged(object sender, EventArgs e)
        {

            if (txtPass.Text != "")
            {
                errorProvider1.SetError(txtPass, "");
            }
        }

        private void txtUsername_TextChanged(object sender, EventArgs e)
        {
            if (txtUsername.Text != "")
            {
                errorProvider1.SetError(txtUsername, "");
            }
        }

        private void txtpassnew_TextChanged(object sender, EventArgs e)
        {
            if (txtpassnew.Text != "")
            {
                errorProvider1.SetError(txtpassnew, "");
            }
        }

        private void txtrepassnew_TextChanged(object sender, EventArgs e)
        {
            if (txtrepassnew.Text != "")
            {
                errorProvider1.SetError(txtrepassnew, "");
            }
        }

        private void frm_ChangePassword_Load(object sender, EventArgs e)
        {
            if (user != null)
            {
                txtUsername.Text = user.Username;
                txtUsername.Enabled = false;
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}